﻿Imports System.ComponentModel
Imports Fountainhead.ViewModel.Controls
Imports FountainHead.ViewModel

Namespace Controls
    Public Class Adder
        Inherits BaseControl
        Public Sub New(ByVal vm As AdderViewModel)
            MyBase.New(vm)
            InitializeComponent()

            'viewModelValue = vm
            terminal1Value = New Terminal(Me, New Point(LineShape3.X1, LineShape3.Y1), True)
            terminal2Value = New Terminal(Me, New Point(LineShape4.X1, LineShape4.Y1), True)
            terminal3Value = New Terminal(Me, New Point(LineShape5.X2, LineShape5.Y2), False)
            AddTerminal(terminal1Value)
            AddTerminal(terminal2Value)
            AddTerminal(terminal3Value)

            terminal1Value.Current = ViewModel.InputCurrent1
            terminal2Value.Current = ViewModel.InputCurrent2
            terminal3Value.Current = ViewModel.OutputCurrent

            AddHandler ViewModel.PropertyChanged, AddressOf ViewModelPropertyChangedHandler
            AddHandler InputTerminal1.PropertyChanged, AddressOf Terminal1PropertyChangedHandler
            AddHandler InputTerminal2.PropertyChanged, AddressOf Terminal2PropertyChangedHandler

            Name = "Adder" & counter.ToString()
            counter += 1

        End Sub
        Private Shared counter = 0
        Private Sub BindControl()

        End Sub

        'Private viewModelValue As AdderViewModel
        Public Overloads ReadOnly Property ViewModel() As AdderViewModel
            Get
                Return MyBase.ViewModel
            End Get
        End Property

        Private terminal1Value As Terminal
        Public Property InputTerminal1() As Terminal
            Get
                Return terminal1Value
            End Get
            Set(ByVal value As Terminal)
                terminal1Value = value
            End Set
        End Property

        Private terminal2Value As Terminal
        Public Property InputTerminal2() As Terminal
            Get
                Return terminal2Value
            End Get
            Set(ByVal value As Terminal)
                terminal2Value = value
            End Set
        End Property

        Private terminal3Value As Terminal
        Public ReadOnly Property OutputTerminal() As Terminal
            Get
                Return terminal3Value
            End Get
        End Property

        Private Sub ViewModelPropertyChangedHandler(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
            Select Case e.PropertyName
                Case "Input1"
                    terminal1Value.Current = ViewModel.InputCurrent1
                Case "Input2"
                    terminal2Value.Current = ViewModel.InputCurrent2
                Case "Output"
                    terminal3Value.Current = ViewModel.OutputCurrent

            End Select
            ClearTerminal()
            AddTerminal(terminal1Value)
            AddTerminal(terminal2Value)
            AddTerminal(terminal3Value)
        End Sub
        Private Sub Terminal1PropertyChangedHandler(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
            Select Case e.PropertyName
                Case "Current"
                    ViewModel.InputCurrent1 = InputTerminal1.Current

            End Select
        End Sub

        Private Sub Terminal2PropertyChangedHandler(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
            Select Case e.PropertyName
                Case "Current"
                    ViewModel.InputCurrent2 = InputTerminal2.Current

            End Select
        End Sub
    End Class
End Namespace